package org.programmers.anonymous.fightrobots.dao.impl;

import java.util.List;

import org.programmers.anonymous.fightrobots.dao.IDeffenseDao;
import org.programmers.anonymous.fightrobots.model.Deffense;
import org.programmers.anonymous.fightrobots.model.IDeffense;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Implements the IDeffenseDao Interface
 * 
 * @author Daniel Cruz, Francisco Villalba
 *
 */
public class DeffenseDao extends HibernateDaoSupport implements IDeffenseDao {

	@Override
	public int save(IDeffense d) 
	{
		Integer id = null;
		if (Integer.valueOf(d.getIdDeffense()) == -99)
		{
			id = (Integer) getHibernateTemplate().save(d);
			d.setIdDeffense(id);

		}
		else
		{
			getHibernateTemplate().saveOrUpdate(d);
			id = d.getIdDeffense();
		}
		return id;
	}

	@Override
	public IDeffense loadDeffense(int Id) 
	{
		Deffense d = getHibernateTemplate().get(Deffense.class, Id);
		return d;
	}

	@Override
	public boolean delete(IDeffense d) 
	{
		try {
			getHibernateTemplate().delete(d);
			return true;
		}catch(Exception hibernateException){
			return false;
		}
	}

	@Override
	public List<IDeffense> getDeffenseList() 
	{
		return  getHibernateTemplate().find("from Deffense");
	}

	@Override
	public void deleteAllDeffenses() 
	{
		for (IDeffense deffense : getDeffenseList())
		{
			delete(deffense);
		}
		
	}

}
